import { Router } from 'express';
const ecc = require('eosjs-ecc');
import logger from './log'
const Eos = require('eosjs')

const routes = Router();
//{"pub":"EOS8UTubtE7pxsx9aaiGEsbYvV2WrAvrtu158DXmHnkgfthgSWbs3",
//"pri":"5HvGMPH7mvSPA8ggzpCkRb9uXaraz8eHgHwJXJmqUwqhhQmCFvv"}
routes.post('/dice/', (req,res,next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
   res.header("Access-Control-Allow-Headers", "X-Requested-With");
   res.header('Access-Control-Allow-Headers', 'Content-Type');
  let wif  ='5JgBd5thpHorzMH5Bcf6kAE3hu1BaRUpUw3tD6i6nVHmNARwGk6';  //部署合约的账号签名

  //产生随机seed
  let random_str=Math.random().toString(36).substr(2);
  let expiration_timestamp = Date.now();
  let seed = random_str+expiration_timestamp;
  let seed_hash = ecc.sha256(seed);

  //修正过期时间
  expiration_timestamp += 300000;//延时5分钟

  //let seed_hash = ecc.sha256(Buffer.from(seed, 'hex'));
  //res.json({hash_key:hash_key});
  let seed_hash_sign = ecc.signHash(seed_hash, wif);
  let result = {
    expiration_timestamp:expiration_timestamp,
    msg:'OK',
    seed:seed_hash,
    signature:seed_hash_sign
  };

  logger.warn(JSON.stringify(result));
  
  res.json({
    expiration_timestamp:expiration_timestamp,
    msg:'OK',
    seed:seed_hash,
    signature:seed_hash_sign
  });
});
export default routes;
